using System;
using System.Linq;
using NUnit.Framework;

namespace Solver
{
	/// <summary>
	/// Find the largest prime factor of a composite number.
	/// </summary>
	public class Problem003 : SelfChecker, IProblem
	{
		public int Id
		{
			get { return 3; }
		}

		public string Solve()
		{
			return Solve(600851475143).ToString();
		}

		[Test]
		public override void Check()
		{
			Assert.AreEqual(29, Solve(13195));
			Assert.AreEqual(6857, Solve(6857));
			Assert.AreEqual(6857, Solve(600851475143));
		}

		private static long Solve(long param)
		{
			var lastPrime = new PrimeNumbersSource("primes-10m.txt").LongPrimes()
				.TakeWhile(p => p <= Convert.ToInt32(Math.Sqrt(param)))
				.LastOrDefault(p => param % p == 0);
			return lastPrime != 0 ? lastPrime : param;
		}
	}
}